<template>
  <div>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
      <el-breadcrumb-item>第1步：选择商品类别</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <!-- 添加数据的表单 -->
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="商品类别" prop="categoryId">
        <el-select v-model="ruleForm.categoryId" placeholder="请选择类别">
          <el-option
              v-for="option in categoryOptions"
              :key="option.value"
              :label="option.label"
              :value="option.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 类别的下拉菜单的选项数据
      categoryOptions: [],
      // 表单
      ruleForm: {
        categoryId: '',
        categoryName: ''
      },
      // 表单规则
      rules: {
        categoryId: [
          { required: true, message: '请选择商品类别', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    // 提交表单：到下一步
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:14001/category/standard?id=' + this.ruleForm.categoryId;
          console.log('url = ' + url);

          let localJwt = JSON.parse(localStorage.getItem('localJwt'));

          this.axios
              .create({
                'headers': {
                  'Authorization': localJwt.token
                }
              })
              .get(url).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              let category = jsonResult.data;
              this.ruleForm.categoryName = category.categoryName;
              localStorage.setItem('localRuleForm', JSON.stringify(this.ruleForm));
              this.$router.push('add-new2');
            } else {
              this.$alert(jsonResult.message, '警告', {
                confirmButtonText: '确定',
                callback: action => {
                  this.loadCategoryTree();
                }
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 加载本地存储的表单数据
    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('localRuleForm');
      let localRuleForm = JSON.parse(localRuleFormString);
      if (localRuleForm && localRuleForm.categoryId) {
        this.ruleForm = localRuleForm;
      }
    },
    // 加载类别树的数据
    loadCategoryTree() {
      let url = 'http://localhost:14001/category/list?queryType=all';
      console.log('url = ' + url);

      let localJwt = JSON.parse(localStorage.getItem('localJwt'));

      this.axios
          .create({
            'headers': {
              'Authorization': localJwt.token
            }
          })
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.categoryOptions = jsonResult.data.list.map(category => {
            return {
              value: category.id,
              label: category.categoryName
            };
          });
        }
      });
    }
  },
  mounted() {
    this.loadLocalRuleForm();
    this.loadCategoryTree();
  }
}
</script>